<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

	/**
	 * Check for valid php settings
	 */
	protected function _initCheckPHPIni()
	{
		if (ini_get('short_open_tag') == false)
		{
			throw new Exception(App_Version::getName() . ' will not work when <em>short_open_tag</em> is disabled');
		}

		if (get_magic_quotes_gpc())
		{
			throw new Exception(App_Version::getName() . ' will not work when <em>magic_quotes_gpc</em> is enabled');
		}

		if (get_magic_quotes_runtime())
		{
			throw new Exception(App_Version::getName() . ' will not work when <em>magic_quotes_runtime</em> is enabled');
		}

		if (ini_get('register_globals'))
		{
			throw new Exception(App_Version::getName() . ' will not work when <em>register_globals</em> is enabled');
		}
	}

	/**
	 * 
	 */
	protected function _initDatabase()
	{
		$this->bootstrap('db');

		if (App_Db_Helper::addTable('user'))
		{
			App_Db_Helper::addColumn('user', 'email', 'VARCHAR(255) NOT NULL');
			App_Db_Helper::addColumn('user', 'password', 'VARCHAR(40) NOT NULL');
			App_Db_Helper::addColumn('user', 'salt', 'VARCHAR(40) NOT NULL');
			App_Db_Helper::addColumn('user', 'name', 'VARCHAR(125) NOT NULL');
		}

		if (App_Db_Helper::addTable('log_system'))
		{
			App_Db_Helper::addColumn('log_system', 'timestamp', 'DATETIME NOT NULL');
			App_Db_Helper::addColumn('log_system', 'message', 'TEXT NOT NULL');
			App_Db_Helper::addColumn('log_system', 'priority', 'INTEGER NOT NULL');
			App_Db_Helper::addColumn('log_system', 'priorityName', 'VARCHAR(15) NOT NULL');
			App_Db_Helper::addColumn('log_system', 'info', 'TEXT NOT NULL');
		}

		if (App_Db_Helper::addTable('log_php'))
		{
			App_Db_Helper::addColumn('log_php', 'timestamp', 'DATETIME NOT NULL');
			App_Db_Helper::addColumn('log_php', 'message', 'TEXT NOT NULL');
			App_Db_Helper::addColumn('log_php', 'priority', 'INTEGER NOT NULL');
			App_Db_Helper::addColumn('log_php', 'priorityName', 'VARCHAR(15) NOT NULL');
			App_Db_Helper::addColumn('log_php', 'errno', 'INTEGER NOT NULL');
			App_Db_Helper::addColumn('log_php', 'file', 'TEXT NOT NULL');
			App_Db_Helper::addColumn('log_php', 'line', 'INTEGER NOT NULL');
		}
	}

	/**
	 * 
	 */
	protected function _initSession()
	{
		$userOptions = array();
		$userOptions['use_only_cookies'] = true;
		$userOptions['save_path'] = APPLICATION_PATH . '/../sessiondata';
		$userOptions['name'] = 'APPSESSID';
		$userOptions['gc_probability'] = 1;
		$userOptions['gc_divisor'] = 1;
		$userOptions['gc_maxlifetime'] = 1440;

		Zend_Session::setOptions($userOptions);
		Zend_Session::setSaveHandler(new App_Session_SaveHandler());
		Zend_Session::start();

		// prevent user to be logged in to long without activity
		$namespace = Zend_Auth::getInstance()->getStorage()->getNamespace();
		$session = new Zend_Session_Namespace($namespace);
		$session->setExpirationSeconds(30 * 60); // 30 minutes time out on auth session
	}

	/**
	 *
	 */
	protected function _initLog()
	{
		$log = new Zend_Log();
		$log->setTimestampFormat('Y-m-d H:i:s');

		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$dbwriter = new Zend_Log_Writer_Db($db, 'log_system');
		$log->addWriter($dbwriter);

		// Add Firebug log writer if not in production
		if ('production' != APPLICATION_ENV)
		{
			$firebugwriter = new Zend_Log_Writer_Firebug();
			$log->addWriter($firebugwriter);
		}

		Zend_Registry::set('log', $log);
	}

	/**
	 * Logs php errors to database
	 */
	protected function _initErrorHandler()
	{
		$log = new Zend_Log();

		$log->setTimestampFormat('Y-m-d H:i:s');

		$columnMap = array(
			 'timestamp' => 'timestamp',
			 'message' => 'message',
			 'priority' => 'priority',
			 'priorityName' => 'priorityName',
			 'errno' => 'errno',
			 'file' => 'file',
			 'line' => 'line',
		);

		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$writer = new Zend_Log_Writer_Db($db, 'log_php', $columnMap);
		$log->addWriter($writer);

		// Add Firebug log writer if not in production
		if ('production' != APPLICATION_ENV)
		{
			$writer = new Zend_Log_Writer_Firebug();
			$log->addWriter($writer);
		}

		$log->registerErrorHandler();
	}

	/**
	 * Bootstrap the view doctype
	 *
	 * @return void
	 */
	protected function _initViewSettings()
	{
		$this->bootstrap('view');
		$view = $this->getResource('view');

		// set defaults for headTitle
		$view->headTitle()->setSeparator(' :: ');
		$view->headTitle()->setDefaultAttachOrder(Zend_View_Helper_Placeholder_Container::PREPEND);
		$view->headTitle(App_Version::getName());
	}

}